package com.ssy.system.controller;

import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.*;
import com.ssy.system.pojo.Permission;
import com.ssy.system.pojo.User;
import com.ssy.system.service.IPermissionService;
import com.ssy.system.vo.PermissionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 左边的导航菜单
     *
     * @param permissionVo
     * @return
     */
    @RequestMapping("/loadIndexLeftMenuTreeJson")
    public List<TreeNode> loadIndexLeftMenuTreeJson(PermissionVo permissionVo, HttpSession session) {
        permissionVo.setType(Constant.TYPE_MENU);
        permissionVo.setAvailable(Constant.AVAILABLE_TRUE);
        User user = (User) session.getAttribute(Constant.SESSION_CURRENT_USER);
        List<Permission> permissions;
        if (user.getType() == Constant.USER_TYPE_SUPER) {
            permissions = this.permissionService.queryAllPermissionForList(permissionVo);
        } else {
            permissions = this.permissionService.queryPermissionBuUserIdForList(permissionVo, user.getId());
        }
        List<TreeNode> treeNodes = new ArrayList<>();
        int type = -1;
        for (Permission p : permissions) {
            TreeNode treeNode = new TreeNode();
            if (p.getOpenType() == null) {
                type = 0;
            } else if (p.getOpenType().equals(Constant.OPEN_TYPE)) {
                type = 1;
            }
            BeanUtils.copyProperties(p, treeNode);
            treeNode.setType(type);
            treeNode.setParentId(p.getPid());
            treeNodes.add(treeNode);
        }
        return TreeNodeBuilder.build(treeNodes, 1);
    }


    /**
     * 菜单管理的树形菜单
     *
     * @param permissionVo permissionVo
     * @return data
     */
    @GetMapping("/loadTreeMenu")
    public Result loadTreeMenu(PermissionVo permissionVo) {
        permissionVo.setAvailable(Constant.AVAILABLE_TRUE);
        permissionVo.setType(Constant.TYPE_MENU);
        List<Permission> permissions = this.permissionService.queryAllPermissionForList(permissionVo);

        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        for (Permission Menu : permissions) {
            TreeNode treeNode = new TreeNode();
            BeanUtils.copyProperties(Menu, treeNode);
            treeNode.setParentId(Menu.getPid());
            treeNodes.add(treeNode);
        }
        List<TreeNode> build = TreeNodeBuilder.build(treeNodes, 0);
        return new Result(build);
    }


    /**
     * 菜单管理数据表格菜单数据
     * 分页查询
     *
     * @param permissionVo permissionVo
     * @return data
     */
    @GetMapping("/loadTableMenu")
    public PageResult loadTableMenu(PermissionVo permissionVo) {
        permissionVo.setType(Constant.TYPE_MENU);
        return this.permissionService.loadAllpermission(permissionVo);
    }

    /**
     * 加载最大的排序码
     *
     * @return
     */
    @PostMapping("/loadMaxOrderNum")
    public Result loadMaxOrderNum() {
        Integer maxOrderNum = this.permissionService.selectMaxOrderNum() + 1;
        return new Result(maxOrderNum);
    }

    /**
     * 添加菜单
     *
     * @param permission
     * @return
     */
    @PostMapping("/addMenu")
    public Result addMenu(Permission permission) {
        if ("1".equals(permission.getOpenType())) {
            permission.setOpenType(Constant.OPEN_TYPE);
        } else {
            permission.setOpenType(null);
        }
        permission.setType(Constant.TYPE_MENU);
        int flag = this.permissionService.insertMenu(permission);
        return flag > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    /**
     * 修改菜单
     *
     * @param permission
     * @return
     */
    @PostMapping("/updateMenu")
    public Result updateMenu(Permission permission) {
        if ("1".equals(permission.getOpenType())) {
            permission.setOpenType(Constant.OPEN_TYPE);
        } else {
            permission.setOpenType(null);
        }
        int i = this.permissionService.updateMenuById(permission);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }


    /**
     * 根据id删除菜单
     *
     * @param id 菜单ID
     * @return
     */
    @DeleteMapping("/deleteMenu/{id}")
    public Result deleteMenu(@PathVariable Integer id) {
        int i = this.permissionService.deleteMenuById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    /**
     * 检查菜单是否有父级菜单
     *
     * @param id 菜单ID
     * @return
     */
    @PostMapping("/checkCurrentMenuHasChild/{id}")
    public Result checkCurrentMenuHasChild(@PathVariable Integer id) {
        int number = this.permissionService.checkCurrentMenuHasChild(id);
        return new Result(number);
    }


}
